package com.runen.infinitemovement.adapter;

import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.model.entity.MyTeamPriceEntity;
import com.runen.infinitemovement.tool.CommonUtil;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class MyTeamPriceAdapter extends BaseQuickAdapter<MyTeamPriceEntity, BaseViewHolder> {
    private Context context;
    private OnClick onClick;
    private EditText et_input;

    public MyTeamPriceAdapter(Context context) {
        super(R.layout.item_my_team_price);
        this.context = context;
    }

    public void setOnClick(OnClick onClick) {
        this.onClick = onClick;
    }

    public void setInputFocus() {
        if (et_input != null) {
            et_input.setFocusable(true);
            et_input.setFocusableInTouchMode(true);
            et_input.requestFocus();
            et_input.findFocus();
            CommonUtil.showKeyboard(et_input);
        }
    }

    public String getEdittext() {
        return et_input != null ? et_input.getText().toString() : "";
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, MyTeamPriceEntity integer) {
        TextView tv_count = baseViewHolder.findView(R.id.tv_count);
        ImageView iv_select = baseViewHolder.findView(R.id.iv_select);
        RelativeLayout rl_normal = baseViewHolder.findView(R.id.rl_normal);
        RelativeLayout rl_input = baseViewHolder.findView(R.id.rl_input);
        et_input = baseViewHolder.findView(R.id.et_input);
        ImageView iv_select_input = baseViewHolder.findView(R.id.iv_select_input);

        if (integer.number != 0) {
            tv_count.setText(integer.number + "");
            rl_normal.setVisibility(View.VISIBLE);
            rl_input.setVisibility(View.GONE);
            iv_select.setVisibility(integer.isSelect ? View.VISIBLE : View.GONE);
            rl_normal.setBackgroundResource(integer.isSelect ? R.drawable.bg_f63e5a_radius_4dp_stroke_1dp : R.drawable.bg_fff8f8_4dp);
        } else {
            rl_normal.setVisibility(View.GONE);
            rl_input.setVisibility(View.VISIBLE);
            iv_select_input.setVisibility(integer.isSelect ? View.VISIBLE : View.GONE);
            rl_input.setBackgroundResource(integer.isSelect ? R.drawable.bg_f3f7ff_radius_4dp_stroke_1dp : R.drawable.bg_f3f7ff_4dp);
            if (integer.isSelect) {
                setInputFocus();
            }
            et_input.setFocusable(false);

            et_input.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onClick != null)
                        onClick.editClick();
                }
            });
        }
    }

    public interface OnClick {
        void editClick();
    }
}
